package database;

import Entity.Single;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 *  @author BinhNV_A04911
 */
public class SingleManagerDB {
    private CallableStatement cstmt;
    private java.sql.Connection conn;
    private ResultSet rs;

    public SingleManagerDB(java.sql.Connection conn) {
        this.conn = conn;
    }
    
    public void loadData(SingleManagerDB markManager) {
        try {
            cstmt = conn.prepareCall("{call sp_GetSingle}");
            rs =  cstmt.executeQuery();
            while(rs.next()) {
                int SingleID = rs.getInt("SingleID");
                int EmployeeID = rs.getInt("EmployeeID");
                Date StartDate = rs.getDate("StartDate");
                Date ReturnedDate = rs.getDate("ReturnedDate");
                String studentID = rs.getString("StudentID");
                String Reasons = rs.getString("Reasons");
                String ReasonsFact = rs.getString("ReasonsFact");
             
                boolean status = rs.getBoolean("Status");
                Single mark = new Single(SingleID, EmployeeID, StartDate, ReturnedDate, Reasons, status, ReasonsFact);
                markManager.add(mark);
            }
            rs.close();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(SingleManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void deleteSingle(Single obj) {
        try {
            cstmt = conn.prepareCall("{call sp_DeleteSingle(?,?)}");
            cstmt.setInt(1, obj.getSingleID());
            cstmt.setInt(2, obj.getEmployeeID());
            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(SingleManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void insertSingle(Single obj) {
        try {
            cstmt = conn.prepareCall("{call sp_InsertSingle(?,?,?,?,?,?,?)}");
            int SingleID = rs.getInt("SingleID");
                int EmployeeID = rs.getInt("EmployeeID");
                Date StartDate = rs.getDate("StartDate");
                Date ReturnedDate = rs.getDate("ReturnedDate");
                String studentID = rs.getString("StudentID");
                String Reasons = rs.getString("Reasons");
                String ReasonsFact = rs.getString("ReasonsFact");
            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(SingleManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void updateSingle(Single obj) {
        try {
            cstmt = conn.prepareCall("{call sp_UpdateSingle(?,?,?,?,?,?,?)}");
                 int SingleID = rs.getInt("SingleID");
                int EmployeeID = rs.getInt("EmployeeID");
                Date StartDate = rs.getDate("StartDate");
                Date ReturnedDate = rs.getDate("ReturnedDate");
                String studentID = rs.getString("StudentID");
                String Reasons = rs.getString("Reasons");
                String ReasonsFact = rs.getString("ReasonsFact");

                boolean status = rs.getBoolean("Status");
            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(SingleManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

